<#assign tit>私教课程预约</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function getmc() {
    var fm = getEl("fm_input");
    var c = fm.cardCode.value;
    if(c!="") {
      getJson("getMemberContract?cardCode="+c, function(data){
        if(data.ret) {
          var mb = data.data;
          fm.cardCode.value = mb.cardCode;
          fm.memberCode.value = mb.memberCode;
          fm.memberName.value = mb.memberName;
          fm.contractCode.value = "";
          addc(mb.contract);
          if(mb.contract.length==0) {
            window.alert("会员没有有效的私教合同");
          }
        } else {
          window.alert(data.msg);
          cls();
        }
      });
    } else {
      cls();
    }
  }

  function cls() {
    var fm = getEl("fm_input");
    //fm.cardCode.value = "";
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.contractCode.value = "";
    addc([]);
  }

  function addc(cl) {
    var i, arr = [];
    arr.push('<table id="tb_list" class="list">'
        + '<tr>'
        + '<th width="7%">选择</th>'
        + '<th width="15%">合同号</th>'
        + '<th width="30%">课程</th>'
        + '<th width="15%">教练</th>'
        + '<th width="12%">剩余课时</th>'
        + '<th width="20%">有效期</th>'
        + '</tr>');
    for(i=0; i<cl.length; i++) {
      var c = cl[i];
      <#if sysUser.userInfo.groupCode?? && sysUser.userInfo.groupCode='G10'>if(c.coachCode.toLowerCase()!='${sysUser.userCode!}'.toLowerCase()) continue;</#if>
      arr.push('<tr class="line'+(i%2?'1':'2')+'" onclick="trsel(this);">'
          + '<td class="no"><input type="radio" name="sel" value="'+c.contractCode+'" /></td>'
          + '<td>'+c.contractCode+'</td>'
          + '<td>'+c.ptCodeName+'</td>'
          + '<td>'+c.coachName+'</td>'
          + '<td>'+c.leftQuantity+'</td>'
          + '<td>'+c.beginTime+'～'+c.endTime+'</td>'
          + '</tr>');
    }
    arr.push('</table>')
    var div = getEl("div_list");
    if(div) div.innerHTML = arr.join("");
  }

  function trsel(tr) {
    tr.firstChild.firstChild.checked = true;
  }

  var _isav = false;
  function save() {
    if(_isav) return false;
    var fm = getEl("fm_input");
    if(fm.memberCode.value=="") {
      window.alert("请输入会员卡号");
      return false;
    }
    var cbs = document.getElementsByName("sel");
    if(!cbs || cbs.length==0) {
      window.alert("会员没有有效的私教合同");
      return false;
    }
    var i, cc = null;
    for(i=0; i<cbs.length; i++) {
      if(cbs[i].checked) {
        cc = cbs[i].value;
        break;
      }
    }
    if(!cc) {
      window.alert("请选择课程");
      return false;
    }
    fm.contractCode.value = cc;
    if(fm.date.value<'${date0!}') {
      window.alert("不能预约以前的日期");
      return false;
    }
    _isav = true;
    postForm(fm.action, fm, saveRet);
  }

  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
      if(window.parent && window.parent.addOk)
        window.parent.addOk();
      cancel();
    } else {
      _isav = false;
    }
  }

  function cancel() {
    if(window.parent && window.parent.closeWin)
      window.parent.closeWin();
    else
      window.close();
  }
</script>

<div class="search">
  <form id="fm_input" method="post" action="_add" onsubmit="return save();">
    <input type="hidden" name="contractCode" value="">
    <dl>
      <dt>会员卡号</dt>
      <dd><input type="text" name="cardCode" value="${cardCode!}" style="width:116px;" onchange="getmc();" maxlength="20"></dd>
      <dd class="button"><input type="button" value="查询" onclick="getmc();" /></dd>
    </dl>
    <dl>
      <dt>会员姓名</dt>
      <dd><input type="text" name="memberName" value="" style="width:116px;" class="disable" readonly></dd>
      <dt>会员编号</dt>
      <dd><input type="text" name="memberCode" value="" style="width:136px;" class="disable" readonly></dd>
    </dl>
    <dl>
      <dt>上课日期</dt>
      <dd><input type="text" name="date" value="${date!}" class="date" maxlength="10" onfocus="cal(this);"></dd>
      <dt>上课时间</dt>
      <dd><select name="hh"><#list 8..22 as h><option value="${h}"<#if hh?? && hh=h> selected</#if>>${h}</option></#list></select>
        : <select name="mm"><#list 0..59 as m><#if m%10=0><option value="${m}"<#if mm?? && mm=m> selected</#if>>${m}</option></#if></#list></select></dd>
    </dl>
  </form>
</div>

<div id="div_list">
  <table class="list">
    <tr>
      <th width="7%">选择</th>
      <th width="35%">课程</th>
      <th width="20%">教练</th>
      <th width="13%">剩余课时</th>
      <th width="25%">有效期</th>
    </tr>
  </table>
</div>

<div class="button"><input type="button" value="确定" onclick="save();"><input type="button" value="取消" onclick="cancel();"></div>
<#if cardCode?? && cardCode?length gt 0><script>getmc();</script></#if>
</@page.html>